package com.choosefine.springboot.devops.base.moudle.mp;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.io.IOException;
import java.util.Date;

/**
 * Created by Administrator on 2017/2/12 0012.
 */
public class Xml {

    public static String resultTextXML(String fromUser,String toUser,String content) throws IOException {
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("<xml>\n" +
                "    <ToUserName><![CDATA[${toUser}]]></ToUserName>\n" +
                "    <FromUserName><![CDATA[${fromUser}]]></FromUserName>\n" +
                "    <CreateTime>"+new Date().getTime()+"</CreateTime>\n" +
                "    <MsgType><![CDATA[text]]></MsgType>\n" +
                "    <Content><![CDATA[${content}]]></Content>\n" +
                "</xml>");
        t.binding("toUser", fromUser);
        t.binding("fromUser",toUser );
        t.binding("content",content);
        String str = t.render();
        return str;
    }

    public static String resultImgXML(String fromUser, String toUser, String picurl, String title, String url) throws IOException {
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("<xml>\n" +
                "        <ToUserName><![CDATA[${toUser}]]></ToUserName>\n" +
                "        <FromUserName><![CDATA[${fromUser}]]></FromUserName>\n" +
                "        <CreateTime>"+new Date().getTime()+"</CreateTime>\n" +
                "        <MsgType><![CDATA[news]]></MsgType>\n" +
                "        <ArticleCount>1</ArticleCount>\n" +
                "        <Articles>\n" +
                "        <item>\n" +
                "        <Title><![CDATA[${title}]]></Title>\n" +
                "        <Description><![CDATA[abbbb]]></Description>\n" +
                "        <PicUrl><![CDATA[${picurl}]]></PicUrl>\n" +
                "        <Url><![CDATA[${url}]]></Url>\n" +
                "        </item>\n" +
                "        </Articles>\n" +
                "        </xml>");
        t.binding("toUser", fromUser);
        t.binding("fromUser",toUser );
        t.binding("title",title);
        t.binding("picurl",picurl);
        t.binding("url",url);
        String str = t.render();
        return str;

    }

}
